1.1 NumPy - Tablice

Wprowadzenie

Głównym obiektem w NumPy jest jednorodna, wielowymiarowa tablica. Przykładem takiej tablicy jest macierz x.

Macierz $x = \begin{pmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{pmatrix}$ można zapisać jako:


In [1]:
import numpy as np

x = np.array([[1,2,3],[4,5,6],[7,8,9]])
print x


[[1 2 3]
 [4 5 6]
 [7 8 9]]

In [2]:
x.shape


Out[2]:
(3, 3)

Tablice typu array mają wiele przydatnych wbudowanych metod.


In [3]:
x.sum(axis=0)


Out[3]:
array([12, 15, 18])

In [4]:
x.sum(axis=1)


Out[4]:
array([ 6, 15, 24])

In [5]:
x.mean(axis=0)


Out[5]:
array([ 4.,  5.,  6.])

In [6]:
x.mean(axis=1)


Out[6]:
array([ 2.,  5.,  8.])

Do tworzenia sekwencji liczbowych jako obiekty typu array należy wykorzystać funkcję arange.


In [7]:
np.arange(10)


Out[7]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [8]:
np.arange(5,10)


Out[8]:
array([5, 6, 7, 8, 9])

In [9]:
np.arange(5,10,0.5)


Out[9]:
array([ 5. ,  5.5,  6. ,  6.5,  7. ,  7.5,  8. ,  8.5,  9. ,  9.5])

Możemy również zmieniać kształt (reshape) wyjścia funkcji arange.


In [10]:
x = np.arange(1,10).reshape(3,3)
x


Out[10]:
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

Podobną funkcją do arange jest linspace która wypełnia wektor określoną liczbą elementów z przedziału o równych automatycznie obliczonych odstępach (w arange sami musimy podać rozmiar kroku.)


In [11]:
x = np.linspace(0,5,5)
x


Out[11]:
array([ 0.  ,  1.25,  2.5 ,  3.75,  5.  ])

Tak jak w przypadku wszystkich funkcji pythonowych możemy uzyskać dodatkowe informacje o funkcjach NumPy za pomocą polecenia help(nazwa.funkcji):


In [12]:
help(np.linspace)


Help on function linspace in module numpy.core.function_base:

linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
    Return evenly spaced numbers over a specified interval.
    
    Returns `num` evenly spaced samples, calculated over the
    interval [`start`, `stop` ].
    
    The endpoint of the interval can optionally be excluded.
    
    Parameters
    ----------
    start : scalar
        The starting value of the sequence.
    stop : scalar
        The end value of the sequence, unless `endpoint` is set to False.
        In that case, the sequence consists of all but the last of ``num + 1``
        evenly spaced samples, so that `stop` is excluded.  Note that the step
        size changes when `endpoint` is False.
    num : int, optional
        Number of samples to generate. Default is 50.
    endpoint : bool, optional
        If True, `stop` is the last sample. Otherwise, it is not included.
        Default is True.
    retstep : bool, optional
        If True, return (`samples`, `step`), where `step` is the spacing
        between samples.
    dtype : dtype, optional
        The type of the output array.  If `dtype` is not given, infer the data
        type from the other input arguments.
    
        .. versionadded:: 1.9.0
    
    Returns
    -------
    samples : ndarray
        There are `num` equally spaced samples in the closed interval
        ``[start, stop]`` or the half-open interval ``[start, stop)``
        (depending on whether `endpoint` is True or False).
    step : float
        Only returned if `retstep` is True
    
        Size of spacing between samples.
    
    
    See Also
    --------
    arange : Similar to `linspace`, but uses a step size (instead of the
             number of samples).
    logspace : Samples uniformly distributed in log space.
    
    Examples
    --------
    >>> np.linspace(2.0, 3.0, num=5)
        array([ 2.  ,  2.25,  2.5 ,  2.75,  3.  ])
    >>> np.linspace(2.0, 3.0, num=5, endpoint=False)
        array([ 2. ,  2.2,  2.4,  2.6,  2.8])
    >>> np.linspace(2.0, 3.0, num=5, retstep=True)
        (array([ 2.  ,  2.25,  2.5 ,  2.75,  3.  ]), 0.25)
    
    Graphical illustration:
    
    >>> import matplotlib.pyplot as plt
    >>> N = 8
    >>> y = np.zeros(N)
    >>> x1 = np.linspace(0, 10, N, endpoint=True)
    >>> x2 = np.linspace(0, 10, N, endpoint=False)
    >>> plt.plot(x1, y, 'o')
    [<matplotlib.lines.Line2D object at 0x...>]
    >>> plt.plot(x2, y + 0.5, 'o')
    [<matplotlib.lines.Line2D object at 0x...>]
    >>> plt.ylim([-0.5, 1])
    (-0.5, 1)
    >>> plt.show()

Tablice mogą sie składać z różnych typów danych (ale tylko jednego typu danych równocześnie, stąd jednorodność).


In [13]:
x = np.array([1,2,3])
print x.dtype
x = np.array([0.1,0.2,0.3])
print x
print x.dtype
x = np.array([1,2,3],dtype='float64')
print x.dtype


int64
[ 0.1  0.2  0.3]
float64
float64

Kilka przydatnych funkcji do tworzenia określonych tablic:

  • zeros
  • ones

In [14]:
x = np.zeros([3,4])
x


Out[14]:
array([[ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.]])

In [15]:
x = np.ones([3,4])
x


Out[15]:
array([[ 1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.]])

Zadania 1.1

  1. Utwórz następującą macierz (za pomocą polecenia składającego się z jednego wiersza) $a = \begin{pmatrix} 1 & 2 & \cdots & 10 \\ 11 & 12 & \cdots & 20 \\ \vdots & \ddots & \ddots & \vdots \\ 91 & 92 & \cdots & 100 \end{pmatrix}$
  2. Za pomocą metod obiektu array określ liczbę elementów, kolumn i wierszy.
  3. Stwórzy wektory średnich po wiersza oraz po kolumnach.
  4. Jaki jest wynik operacji: a[4,:]
  5. Sprawdź i opisz (krótko) działania następujących funkcji:
    • np.log(a)
    • np.cumsum(a)
    • np.rank(a)
    • np.power(a,2)
  6. Stwórz wektor składający się z 50 punktów z przedziału 11 do 23